import 'package:hive/hive.dart';

part 'music.g.dart';

/// 音乐数据模型类
/// 包含音乐的基本信息如标题、艺术家、专辑等
@HiveType(typeId: 0)
class Music {
  /// 音乐唯一标识符
  @HiveField(0)
  final String id;

  /// 音乐标题
  @HiveField(1)
  final String title;

  /// 艺术家名称
  @HiveField(2)
  final String artist;

  /// 专辑名称
  @HiveField(3)
  final String? album;

  /// 音乐文件路径
  @HiveField(4)
  final String filePath;

  /// 音乐时长（毫秒）
  @HiveField(5)
  final int duration;

  /// 发行年份
  @HiveField(14)
  final int? year;

  /// 音乐流派
  @HiveField(15)
  final String? genre;

  /// 专辑封面路径
  @HiveField(6)
  final String? coverArt;

  /// 文件大小（字节）
  @HiveField(7)
  final int? fileSize;

  /// 比特率
  @HiveField(8)
  final int? bitRate;

  /// 采样率
  @HiveField(9)
  final int? sampleRate;

  /// 创建时间
  @HiveField(10)
  final DateTime createdAt;

  /// 更新时间
  @HiveField(11)
  final DateTime updatedAt;

  /// 是否被收藏
  @HiveField(12)
  final bool isFavorite;

  /// 播放次数
  @HiveField(13)
  final int playCount;

  /// 构造函数
  Music({
    required this.id,
    required this.title,
    required this.artist,
    this.album,
    required this.filePath,
    required this.duration,
    this.coverArt,
    this.fileSize,
    this.bitRate,
    this.sampleRate,
    required this.createdAt,
    required this.updatedAt,
    this.isFavorite = false,
    this.playCount = 0,
    this.year,
    this.genre,
  });

  /// 从JSON创建Music对象
  factory Music.fromJson(Map<String, dynamic> json) {
    return Music(
      id: json['id'] as String,
      title: json['title'] as String,
      artist: json['artist'] as String,
      album: json['album'] as String?,
      filePath: json['file_path'] as String,
      duration: json['duration'] as int,
      coverArt: json['cover_art'] as String?,
      fileSize: json['file_size'] as int?,
      bitRate: json['bit_rate'] as int?,
      sampleRate: json['sample_rate'] as int?,
      createdAt: DateTime.fromMillisecondsSinceEpoch(json['created_at'] as int),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(json['updated_at'] as int),
      isFavorite: json['is_favorite'] is int 
          ? (json['is_favorite'] as int) == 1 
          : json['is_favorite'] as bool? ?? false, // 处理int和bool两种格式
      playCount: json['play_count'] as int? ?? 0,
      year: json['year'] as int?,
      genre: json['genre'] as String?,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'artist': artist,
      'album': album,
      'file_path': filePath,
      'duration': duration,
      'cover_art': coverArt,
      'file_size': fileSize,
      'bit_rate': bitRate,
      'sample_rate': sampleRate,
      'created_at': createdAt.millisecondsSinceEpoch,
      'updated_at': updatedAt.millisecondsSinceEpoch,
      'is_favorite': isFavorite ? 1 : 0, // 将bool转换为int
      'play_count': playCount,
      'year': year,
      'genre': genre,
    };
  }

  /// 复制并修改部分属性
  Music copyWith({
    String? id,
    String? title,
    String? artist,
    String? album,
    String? filePath,
    int? duration,
    String? coverArt,
    int? fileSize,
    int? bitRate,
    int? sampleRate,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? isFavorite,
    int? playCount,
    int? year,
    String? genre,
  }) {
    return Music(
      id: id ?? this.id,
      title: title ?? this.title,
      artist: artist ?? this.artist,
      album: album ?? this.album,
      filePath: filePath ?? this.filePath,
      duration: duration ?? this.duration,
      coverArt: coverArt ?? this.coverArt,
      fileSize: fileSize ?? this.fileSize,
      bitRate: bitRate ?? this.bitRate,
      sampleRate: sampleRate ?? this.sampleRate,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      isFavorite: isFavorite ?? this.isFavorite,
      playCount: playCount ?? this.playCount,
      year: year ?? this.year,
      genre: genre ?? this.genre,
    );
  }

  /// 格式化时长为可读字符串
  String get formattedDuration {
    final minutes = duration ~/ 60000;
    final seconds = (duration % 60000) ~/ 1000;
    return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }

  /// 获取文件大小的可读字符串
  String get formattedFileSize {
    if (fileSize == null) return 'Unknown';
    final sizeInMB = fileSize! / (1024 * 1024);
    return '${sizeInMB.toStringAsFixed(1)} MB';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Music && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'Music(id: $id, title: $title, artist: $artist, album: $album)';
  }
}
